﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SpotiKat.Spotify.Core;
using System.Xml.Linq;

namespace SpotiKat.Spotify.Manager
{
    public class SearchManager
    {
        public static SearchManager Instance { get { return new SearchManager(); } }

        public ArtistSearchResult SearchArtist(string query, int? pageIndex)
        {
            if (pageIndex == null)
            {
                pageIndex = 1;
            }

            ArtistSearchResult result = new ArtistSearchResult();

            string url = Settings.Default.ArtistSearchServiceUrl;

            url = string.Format(url, System.Web.HttpUtility.UrlEncode(query), pageIndex.Value);

            SpotifyManager spotifyManager = SpotifyManager.Instance;

            XDocument response = spotifyManager.GetResponse(url);

            XElement xelement = response.Root;

            result.StartPage = pageIndex.Value;
            result.SearchTerms = query;

            if (xelement != null)
            {
                int? totalResults = null;
                int? startIndex = null;
                int? itemsPerPage = null;
                spotifyManager.GetSearchResultMetaData(xelement, out totalResults, out startIndex, out itemsPerPage);
                result.TotalResults = totalResults;
                result.StartIndex = startIndex;
                result.ItemsPerPage = itemsPerPage;
                result.Artists = spotifyManager.GetArtists(xelement);
                if (result.TotalResults != null && result.TotalResults.Value > 0)
                {
                    result.Success = true;
                }
            }

            return result;
        }

        public AlbumSearchResult SearchAlbum(string query, int? pageIndex)
        {
            if (pageIndex == null)
            {
                pageIndex = 1;
            }

            AlbumSearchResult result = new AlbumSearchResult();

            string url = Settings.Default.AlbumSearchServiceUrl;

            url = string.Format(url, System.Web.HttpUtility.UrlEncode(query), pageIndex.Value);

            SpotifyManager spotifyManager = SpotifyManager.Instance;

            XDocument response = spotifyManager.GetResponse(url);

            XElement xelement = response.Root;

            result.StartPage = pageIndex.Value;
            result.SearchTerms = query;

            if (xelement != null)
            {
                int? totalResults = null;
                int? startIndex = null;
                int? itemsPerPage = null;
                spotifyManager.GetSearchResultMetaData(xelement, out totalResults, out startIndex, out itemsPerPage);
                result.TotalResults = totalResults;
                result.StartIndex = startIndex;
                result.ItemsPerPage = itemsPerPage;
                result.Albums = spotifyManager.GetAlbums(xelement);
                if (result.TotalResults != null && result.TotalResults.Value > 0)
                {
                    result.Success = true;
                }
            }

            return result;
        }

        public TrackSearchResult SearchTrack(string query, int? pageIndex)
        {
            if (pageIndex == null)
            {
                pageIndex = 1;
            }

            TrackSearchResult result = new TrackSearchResult();

            string url = Settings.Default.TrackSearchServiceUrl;

            url = string.Format(url, System.Web.HttpUtility.UrlEncode(query), pageIndex.Value);

            SpotifyManager spotifyManager = SpotifyManager.Instance;

            XDocument response = spotifyManager.GetResponse(url);

            XElement xelement = response.Root;

            result.StartPage = pageIndex.Value;
            result.SearchTerms = query;

            if (xelement != null)
            {
                int? totalResults = null;
                int? startIndex = null;
                int? itemsPerPage = null;
                spotifyManager.GetSearchResultMetaData(xelement, out totalResults, out startIndex, out itemsPerPage);
                result.TotalResults = totalResults;
                result.StartIndex = startIndex;
                result.ItemsPerPage = itemsPerPage;
                result.Tracks = spotifyManager.GetTracks(xelement);
                if (result.TotalResults != null && result.TotalResults.Value > 0)
                {
                    result.Success = true;
                }
            }

            return result;
        }
    }
}
